<?php

namespace api\modules\v1\models\userReceipt;

use api\modules\v1\models\Common;
use api\modules\v1\models\user\User;
use common\models\Functions;
use common\models\Status;
use yii\base\Exception;

/**
 * Class EmployeeInfo
 * @package api\modules\v1\models\employee
 */
class Receipt extends Common
{
    public function receipt()
    {
        try {
            $user = (new User())->checkUser();
            
            $data = static::getReceiptByUserId($user['id']);

            $data['bank'] = empty($data['bank']) ? [
                "card_no" => '',
                "bank_name" => '',
                "bank_holder" => '',
                "card_photo" => '',
                "card_photo_path" => '',
            ] : $data['bank'];
            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
    
    public static function getReceiptByUserId($userId = 0)
    {

        $data['bank'] = ReceiptBank::getOneByWhere([
            'user_id' => $userId,
            'status' => Status::AUDIT_RECEIPT_STATUS_SUCCESS
        ], ['card_no', 'bank_name', "bank_holder",'card_photo', "status"]);


        if (!empty($data['bank'])) {
            $data['bank']['card_photo_path'] = $data['bank']['card_photo'];
            $data['bank']['card_photo'] = empty($data['bank']['card_photo']) ? null : Common::getImages($data['bank']['card_photo']);
        }

        return $data;
    }

    public static function formatReceipt($userId = 0) {
        $receipt = static::getReceiptByUserId($userId);
        $res = [];
        foreach ($receipt as $k => $v) {
            switch ($k) {
                case "alipay":
                    $type = 1;
                    break;
                case "bank":
                    $type = 2;
                    break;
                case "wechat":
                    $type = 3;
                    break;
                case "ether":
                    $type = 4;
                    break;
            }
            if (!empty($v)) {
                $res[] = ["type" => $type, "data" => $v];
            }
        }

        return $res;
    }
    public function afterRegister($user = 0)
    {
        $send = \common\models\Common::getCommonByKey('register_send_hashrate');
        
        if ($send > 0) {
            User::updateAllCounters(['hashrate' => $send], ['id' => $user]);
        }
    }
}
